﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Futajma.MigrationStudio.DataDefinition
{
    public class ItemOperation
    {
        public static void SetItemColorForChilds(TreeNode item, bool targetTreeItem, int currentProjectVersion)
        {
            SetItemColor(item, targetTreeItem, currentProjectVersion);
            foreach(TreeNode child in item.Nodes)
            {                
                SetItemColorForChilds(child, targetTreeItem, currentProjectVersion);
            }
        }

        public static void SetItemColor(TreeNode item, bool targetTreeItem, int currentProjectVersion)
        {
            var data = item.Tag as DataNode;

            var color = SystemColors.ControlText;

            if (targetTreeItem)
            {
                // version check
                if (data.Version != currentProjectVersion)
                {
                    color = Color.Gray;
                }
                    // governance violation
                if (data.GovernanceViolationCollected > 0 || data.SharePointStructureViolationCollected > 0 || data.GovernanceViolation)
                {
                    color = Color.Red;
                }
            }
            else
            {
                // version check
                 if (data.Version != currentProjectVersion)
                {
                    color = Color.Gray;
                }
                 else if (data.NodeDisabled)
                 {
                      color = Color.Gray;
                 }
                // already moved (disabled)
            }
            item.ForeColor = color;
        }

        public static string GeneratePath(TreeNode node, bool flattenStructure)
        {
            var data = node.Tag as DataNode;

            if (node.Parent != null)
            {
                if (flattenStructure)
                {
                    if (data.SharePointType == SharePointType.Folder)
                    {
                        return GeneratePath(node.Parent, flattenStructure);
                    }
                }

                if (data.SharePointType == SharePointType.SiteCollection)
                {
                    return data.TargetPath;
                    //return string.Format("{0}/{1}", data.TargetPath, node.Text);
                }
                else if (data.SharePointType == SharePointType.Site && !string.IsNullOrEmpty(data.TargetPath))
                {
                    return string.Format("{0}/{1}", GeneratePath(node.Parent, flattenStructure), data.TargetPath);
                }
                else if (data.SharePointType == SharePointType.DocLibrary && !string.IsNullOrEmpty(data.TargetPath))
                {
                    return string.Format("{0}/{1}", GeneratePath(node.Parent, flattenStructure), data.TargetPath);
                }
                else
                {
                    return string.Format("{0}/{1}", GeneratePath(node.Parent, flattenStructure), node.Text);
                }
            }
            else
            {
                if (data.SharePointType != SharePointType.Application)
                {
                    return node.Text;
                }
                else
                {
                    return string.Empty;
                }
            }
        }
    }
}
